﻿//var currentNewUnfinishedAuctions;
function autoLoadNextAuction(typeOfAuction,typeView) {
    if (typeOfAuction == 1) {
        $(document).scroll(function () {
            if ($(window).scrollTop() + $(window).height() == $(document).height()) {

                loadNextBuyAuction(typeView);

            }
        });
    } else if (typeOfAuction == 2) {
        $(document).scroll(function () {
            if ($(window).scrollTop() + $(window).height() == $(document).height()) {

                loadNextSellAuction(typeView);

            }
        });
    } else if (typeOfAuction == 3) {
        $(document).scroll(function () {
            if ($(window).scrollTop() + $(window).height() == $(document).height()) {
                loadNextAuction(typeView);                
            }
        });
    }
}
var allProducts;
function jsonDateToNormalDate(jsonDate) {
    var milli = jsonDate.replace(/\/Date\((-?\d+)\)\//, '$1');
    var d = new Date(parseInt(milli));
    return d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getMilliseconds();
    //return d.toLocaleString();
    //return d.toLocaleDateString();
}


function loadNextAuction(typeView) {
    var startedId = $("#lastId").val();
    if (startedId >= 0) {
        $("#BtnNNextAuctions").html("<img src='../../Content/Images/ajax-loader.gif' /> Loading");
        var data2Send = '{"startedId":' + startedId.toString() + '}';
        if (typeView == 1) {
            $.ajax({
                type: "POST",
                url: '/Auction/LoadNextAuctions',
                data: data2Send,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) { //call successfull
                    $("#auctionListDiv").append(data.content);
                    if (data.lastId > 0) {
                        $("#lastId").val(data.lastId);
                    } else {
                        $("#BtnNNextAuctions").remove();
                        //                $("#lastId").remove();
                        $("#lastId").val(-1);
                    }
                    $("#BtnNNextAuctions").html("Read more");
                },
                error: function (xhr) {
                    //error occurred
                }
            });
        } else if (typeView == 2) {
            $.ajax({
                type: "POST",
                url: '/Auction/LoadNextBeingAuctions',
                data: data2Send,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) { //call successfull
                    $("#auctionListDiv").append(data.content);
                    if (data.lastId > 0) {
                        $("#lastId").val(data.lastId);
                    } else {
                        $("#BtnNNextAuctions").remove();
                        //                $("#lastId").remove();
                        $("#lastId").val(-1);
                    }
                    $("#BtnNNextAuctions").html("Read more");
                },
                error: function (xhr) {
                    //error occurred
                }
            });
        }
    }
}




function loadNextBuyAuction(typeView) {
    var startedId = $("#lastId").val();
    if (startedId >= 0) {
        $("#BtnNNextBuyAuctions").html("<img src='../../Content/Images/ajax-loader.gif' /> Loading");
        var data2Send = '{"startedId":' + startedId.toString() + '}';
        if(typeView==1) {
            $.ajax({
                type: "POST",
                url: '/Auction/LoadNextBuyAuctions',
                data: data2Send,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) { //call successfull
                    $("#auctionListDiv").append(data.content);
                    if (data.lastId > 0) {
                        $("#lastId").val(data.lastId);
                    } else {
                        $("#BtnNNextBuyAuctions").remove();
                        //                $("#lastId").remove();
                        $("#lastId").val(-1);
                    }
                    $("#BtnNNextBuyAuctions").html("Read more");
                },
                error: function (xhr) {
                    //error occurred
                }
            });
        }else if(typeView==2){
            $.ajax({
                    type: "POST",
                    url: '/Auction/LoadNextBeingBuyAuctions',
                    data: data2Send,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) { //call successfull
                        $("#auctionListDiv").append(data.content);
                        if (data.lastId > 0) {
                            $("#lastId").val(data.lastId);
                        } else {
                            $("#BtnNNextBuyAuctions").remove();
                            //                $("#lastId").remove();
                            $("#lastId").val(-1);
                        }
                        $("#BtnNNextBuyAuctions").html("Read more");
                    },
                    error: function (xhr) {
                        //error occurred
                    }
                });
        }
    }
}
function loadNextSellAuction(typeView) {
    var startedId = $("#lastId").val();
    if (startedId >= 0) {
        $("#BtnNNextSellAuctions").html("<img src='../../Content/Images/ajax-loader.gif' /> Loading");
        var data2Send = '{"startedId":' + startedId.toString() + '}';
        if (typeView == 1) {
            $.ajax({
                type: "POST",
                url: '/Auction/LoadNextSellAuctions',
                data: data2Send,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) { //call successfull
                    $("#auctionListDiv").append(data.content);
                    if (data.lastId > 0) {
                        $("#lastId").val(data.lastId);
                    } else {
                        $("#BtnNNextSellAuctions").remove();
                        //                $("#lastId").remove();
                        $("#lastId").val(-1);
                    }
                    $("#BtnNNextSellAuctions").html("Read more");
                },
                error: function (xhr) {
                    //error occurred
                }
            });
        } else if (typeView == 2) { 
            $.ajax({
                type: "POST",
                url: '/Auction/LoadNextBeingSellAuctions',
                data: data2Send,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) { //call successfull
                    $("#auctionListDiv").append(data.content);
                    if (data.lastId > 0) {
                        $("#lastId").val(data.lastId);
                    } else {
                        $("#BtnNNextSellAuctions").remove();
                        //                $("#lastId").remove();
                        $("#lastId").val(-1);
                    }
                    $("#BtnNNextSellAuctions").html("Read more");
                },
                error: function (xhr) {
                    //error occurred
                }
            });
        }
    }
}
function updateAuction(auction) {
    var auctionId = auction.AuctionId;
    var updateButton = $("#updateButton" + auctionId);
    if (updateButton.val().toUpperCase() == "UPDATE") {
        updateButton.val("Confirm Update");
        //var newAuctionId = "sellAuction" + auctionId.toString();
        //var auctionDiv = document.getElementById(newAuctionId);
        var pictureDiv = document.getElementById("picture" + auctionId);
        //var userName = document.getElementById("userName" + auctionId);
        var startedDate = document.getElementById("startedDate" + auctionId);
        var finishedDate = document.getElementById("finishedDate" + auctionId);
        var productName = document.getElementById("productName" + auctionId);
        var initialPrice = document.getElementById("initialPrice" + auctionId);
        var detail = document.getElementById("detail" + auctionId);
        startedDate.innerHTML = "<input id='" + startedDate.id + "Input' type='text' value='" + startedDate.innerHTML + "'/><img src='../../External/DatetimePicker/images2/cal.gif' onclick='javascript:NewCssCal(&apos;" + startedDate.id + "Input&apos;,&apos;&apos;,&apos;ddMMyyyy&apos;,&apos;dropdown&apos;,true,&apos;12&apos;,true)' style='cursor:pointer'/>";
        finishedDate.innerHTML = "<input id='" + finishedDate.id + "Input' type='text' value='" + finishedDate.innerHTML + "'/><img src='../../External/DatetimePicker/images2/cal.gif' onclick='javascript:NewCssCal(&apos;" + finishedDate.id + "Input&apos;,&apos;&apos;,&apos;ddMMyyyy&apos;,&apos;dropdown&apos;,true,&apos;12&apos;,true)' style='cursor:pointer'/>";
        var productNodeSelection = document.createElement('select');
        productNodeSelection.id = "productNodeSelection" + auctionId;
        var tempProductName = productName.innerHTML;
        productName.innerHTML = "";
        productName.appendChild(productNodeSelection);
        for (var i = 0; i < allProducts.length; i++) {
            var newProductOption = document.createElement('option');
            newProductOption.value = allProducts[i].ProductId;
            newProductOption.innerHTML = allProducts[i].ProductName;
            productNodeSelection.appendChild(newProductOption);
            if (allProducts[i].ProductName == tempProductName) {
                newProductOption.setAttribute('selected', true);
            }
        }
        initialPrice.innerHTML = "<input type='number' id='" + initialPrice.id + "Input' type='text' value='" + initialPrice.innerHTML + "'/>";
        detail.innerHTML = "<textarea rows='4' cols='70' id='" + detail.id + "Input' >" + detail.innerHTML + "</textarea>";
    } else {
        //var newAuctionId = "sellAuction" + auctionId.toString();
        //var auctionDiv = document.getElementById(newAuctionId);
        var pictureDiv = document.getElementById("picture" + auctionId);
        var startedDate = document.getElementById("startedDate" + auctionId);
        var finishedDate = document.getElementById("finishedDate" + auctionId);
        var productName = document.getElementById("productName" + auctionId);
        var initialPrice = document.getElementById("initialPrice" + auctionId);
        var detail = document.getElementById("detail" + auctionId);
        var productNodeSelection = productName.childNodes[0];
        var selectedProductOption = productNodeSelection[productNodeSelection.selectedIndex];
        var selectedProductName = selectedProductOption.innerHTML;
        var selectedProductId = selectedProductOption.value;
        startedDate.innerHTML = document.getElementById("startedDate" + auctionId + "Input").value;
        finishedDate.innerHTML = document.getElementById("finishedDate" + auctionId + "Input").value;
        productName.removeChild(productNodeSelection);
        productName.appendChild(document.createTextNode(selectedProductName));
        initialPrice.innerHTML = document.getElementById("initialPrice" + auctionId + "Input").value;
        detail.innerHTML = document.getElementById("detail" + auctionId + "Input").value;
        var newAuction = JSON.parse(JSON.stringify(auction));
        newAuction.StartedDate = startedDate.innerHTML;
        newAuction.FinishedDate = finishedDate.innerHTML;
        for (var i = 0; i < allProducts.length; i++) {
            if (allProducts[i].ProductId == selectedProductId) {
                newAuction.Product = allProducts[i];
            }
        }
        newAuction.InitialPrice = initialPrice.innerHTML;
        newAuction.ProductDescription = detail.innerHTML;

        $.ajax({
            type: "POST",
            url: '/Auction/UpdateAuction',
            data: JSON.stringify(newAuction),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processData: false,
            success: function (data) { //call successfull
                if (data.IsOk == 0) {
                    $("#ErrorPanelDiv" + auction.AuctionId).html("Thanh cong");
                    setTimeout(function () {
                        $("#ErrorPanelDiv" + auction.AuctionId).html("");
                    }, 2000);
                    auction = JSON.parse(JSON.stringify(newAuction));
                } else {
                    $("#ErrorPanelDiv" + auction.AuctionId).html(data.ErrorMessage);
                    //                    setTimeout(function () {
                    //                        $("#ErrorPanelDiv" + auction.AuctionId).html("");
                    //                    }, 2000);
                    $("#startedDate" + auction.AuctionId).html(jsonDateToNormalDate(auction.StartedDate));
                    $("#finishedDate" + auction.AuctionId).html(jsonDateToNormalDate(auction.FinishedDate));
                    $("#productName" + auction.AuctionId).html(auction.ProductName);
                    $("#initialPrice" + auction.AuctionId).html(auction.InitialPrice);
                    $("#detail" + auction.AuctionId).html(auction.ProductDescription);
                }
            },
            error: function (xhr) {
                //error occurred
            }
        });
        $("#updateButton" + auction.AuctionId).val("Update");
    }
}

function deleteAuction(auctionId) {
    var data2send='{"auctionId":' + auctionId.toString() + '}';
    $.ajax({
        type: "POST",
        url: '/Auction/DeleteAuction',
        data: data2send,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        //processData: false,
        success: function (data) { //call successfull
            if (data.IsOk == 0) {
                $("#ErrorPanelDiv" + auctionId).html("Thanh cong");
                setTimeout(function () {
                    $("#ErrorPanelDiv" + auctionId).html("");
                }, 2000);
                $("#buyAuction" + auctionId).remove();
            } else {
                $("#ErrorPanelDiv" + auctionId).html("Loi");
                setTimeout(function () {
                    $("#ErrorPanelDiv" + auctionId).html("");
                }, 2000);
            }
        },
        error: function (xhr) {
            //error occurred
        }
    });
}

function slideToggleNewBuyAuctionDiv() {
    $("#newBuyAuctionDiv").slideToggle("slow");
}
function submitAddNewBuyAuction() {
    alert("submitAddNewBuyAuction")
}